//package com.resou.reader.bookdetail.adapter;
//
//import android.graphics.Bitmap;
//import android.graphics.Canvas;
//import android.graphics.LinearGradient;
//import android.graphics.Matrix;
//import android.graphics.Paint;
//import android.graphics.Rect;
//import android.support.annotation.NonNull;
//import android.support.annotation.Nullable;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
//import android.support.v7.widget.RecyclerView.ViewHolder;
//import android.text.TextUtils;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.ImageView;
//import android.widget.LinearLayout;
//import android.widget.TextView;
//
//import com.bumptech.glide.Glide;
//import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
//import com.bumptech.glide.request.RequestOptions;
//import com.bumptech.glide.request.target.SimpleTarget;
//import com.bumptech.glide.request.transition.Transition;
//import com.resou.reader.R;
//import com.resou.reader.api.entry.BookDetailBean;
//import com.resou.reader.api.entry.CommentBean;
//import com.resou.reader.api.entry.TypeBean;
//import com.resou.reader.bookshelf.listener.OnItemClickListener;
//import com.resou.reader.utils.DateUtils;
//import com.resou.reader.utils.log.DLog;
//import com.vansuita.gaussianblur.GaussianBlur;
//
//import java.util.List;
//
///**
// * Created by ${Garrett} on 2018/9/3.
// * Contact me krouky@outlook.com
// */
//public class BookDetailAdapter extends RecyclerView.Adapter {
//
//    private static final int TYPE_BASIC_INFO = 0;
//    private static final int TYPE_COMMENT = 1;//ic_comment_write
//    private static final int TYPE_RECOMMEND = 2;//推荐
//
//    private List<Object> mList;
//    private OnItemClickListener listener;
//
//    public BookDetailAdapter(OnItemClickListener listener) {
//        this.listener = listener;
//    }
//
//    @NonNull
//    @Override
//    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//        ViewHolder viewHolder = null;
//        View view;
//        switch (i) {
//            case TYPE_BASIC_INFO:
//                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail_basic_info, viewGroup, false);
//                viewHolder = new BaseInfoViewHolder(view);
//                break;
//            case TYPE_COMMENT:
//                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail_evaluate, viewGroup, false);
//                viewHolder = new EvaluateViewHolder(view);
//                break;
//            case TYPE_RECOMMEND:
//                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail_horizontal_recycler_view, viewGroup, false);
//                viewHolder = new RecommendHolder(view);
//            default:
//                break;
//        }
//        return viewHolder;
//    }
//
//    private Bitmap scaleClipBitmap(Bitmap resource, int width, int height) {
//        int bpWidth = resource.getWidth();
//        int bpHeight = resource.getHeight();
//
//        int clipHeight = bpHeight;//bpWidth * height / width;
//
//        float scaleWidth = width * 1f / bpWidth;
//        float scaleHeight = height * 1f / clipHeight;
//        Matrix matrix = new Matrix();
//        matrix.postScale(scaleWidth, scaleHeight);
//        Bitmap resultBitmap = Bitmap.createBitmap(resource, 0, bpHeight / 2 - clipHeight / 2, bpWidth, clipHeight, matrix, true);
//        Canvas canvas = new Canvas(resultBitmap);
////        canvas.drawColor(0x55ffffff);
//        int startColor = 0x60fafafa;
//        int endColor = 0xfffafafa;
//        LinearGradient lg = new LinearGradient(0, 0, 0, resultBitmap.getHeight(), startColor, endColor, LinearGradient.TileMode.CLAMP);
//        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        paint.setShader(lg);
//        canvas.drawRect(new Rect(0, 0, resultBitmap.getWidth(), resultBitmap.getHeight()), paint);
//        return resultBitmap;
//    }
//
//    private Bitmap postBitmap;
//    private int width;
//    private int height;
//
//    @Override
//    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
//        int type = getItemViewType(i);
//        switch (type) {
//            case TYPE_BASIC_INFO:
//                BookDetailBean bean = (BookDetailBean) mList.get(i);
//                if (bean == null) {
//                    break;
//                }
//                final BaseInfoViewHolder baseInfoViewHolder = (BaseInfoViewHolder) viewHolder;
//                baseInfoViewHolder.bookBg.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
//                    @Override
//                    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//                        DLog.d(BookDetailAdapter.class.getSimpleName(), "left,right,top,bottom == " + left + "," + top + "," + right + "," + bottom);
//                        if (oldRight == 0 && right != 0 && oldBottom == 0 && bottom != 0) {
//                            width = right - left;
//                            height = bottom - top;
//                            if (postBitmap != null && !postBitmap.isRecycled()) {
//                                Bitmap result = scaleClipBitmap(postBitmap, width, height);
//                                GaussianBlur.with(viewHolder.itemView.getContext()).radius(20).size(790).put(result, baseInfoViewHolder.bookBg);
//                            }
//                        }
//                    }
//                });
//                Glide.with(viewHolder.itemView).applyDefaultRequestOptions(new RequestOptions()
//                        .placeholder(R.mipmap.ic_place)
//                        .error(R.mipmap.ic_place))
//                        .load(bean.getCoverUrl())
//                        .transition(DrawableTransitionOptions.withCrossFade())
//                        .into(baseInfoViewHolder.bookPic);
//                Glide.with(viewHolder.itemView).asBitmap().load(bean.getCoverUrl()).into(new SimpleTarget<Bitmap>() {
//                    @Override
//                    public void onResourceReady(@NonNull final Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                        //baseInfoViewHolder.bookBg.getLayoutParams().width, baseInfoViewHolder.bookBg.getLayoutParams().height
//                        DLog.d(BookDetailAdapter.class.getSimpleName(), "Glide onResourceReady  width:" + width);
//                        if (width > 0 && height > 0) {
//                            Bitmap result = scaleClipBitmap(resource, width, height);
//                            GaussianBlur.with(viewHolder.itemView.getContext()).radius(20).size(790).put(result, baseInfoViewHolder.bookBg);
//                        } else {
//                            postBitmap = resource;
//                        }
//                    }
//                });
//
//                baseInfoViewHolder.bookTitle.setText(bean.getNovelName());
//                baseInfoViewHolder.bookSub.setText(bean.getAuthorName());
//                baseInfoViewHolder.bookWordNum.setText(String.valueOf(bean.getTotalWord() / 10000 + viewHolder.itemView.getContext().getString(R.string.character_unit)));
//                baseInfoViewHolder.parentCategory.setText(bean.getParentCategoryName());
//                baseInfoViewHolder.category.setText(bean.getCategoryName());
////                baseInfoViewHolder.descText.setContent(bean.getNovelDescription());
//
//                if (!TextUtils.isEmpty(bean.getUpdateChapterName()))
//                    baseInfoViewHolder.updateCategory.setText(bean.getUpdateChapterName());
//                baseInfoViewHolder.updateTime.setText(DateUtils.getTheDayFromNow(bean.getUpdateTime()));
//
//                baseInfoViewHolder.commentSum.setText("共" + String.valueOf(bean.getChapterNum()) + "章");
//                baseInfoViewHolder.commentTime.setText(DateUtils.getTheDayFromNow(bean.getUpdateTime()));
//                break;
//            case TYPE_COMMENT:
//                EvaluateViewHolder evaluateViewHolder = (EvaluateViewHolder) viewHolder;
//                if (mList.get(i) == null) {
//                    //暂无评论
//                    evaluateViewHolder.evaluateDate.setVisibility(View.INVISIBLE);
//                    evaluateViewHolder.likeText.setVisibility(View.INVISIBLE);
//                    evaluateViewHolder.userHead.setVisibility(View.INVISIBLE);
//                    evaluateViewHolder.userName.setVisibility(View.INVISIBLE);
//                    evaluateViewHolder.moreEvaluate.setVisibility(View.INVISIBLE);
//                    evaluateViewHolder.evaluateContent.setText("");
//                    evaluateViewHolder.noCommentText.setVisibility(View.VISIBLE);
//                } else {
//                    evaluateViewHolder.evaluateDate.setVisibility(View.VISIBLE);
//                    evaluateViewHolder.likeText.setVisibility(View.VISIBLE);
//                    evaluateViewHolder.userHead.setVisibility(View.VISIBLE);
//                    evaluateViewHolder.userName.setVisibility(View.VISIBLE);
//                    evaluateViewHolder.moreEvaluate.setVisibility(View.VISIBLE);
//                    evaluateViewHolder.noCommentText.setVisibility(View.GONE);
//                    CommentBean commentBean = (CommentBean) mList.get(i);
//                    Glide.with(viewHolder.itemView).load(commentBean.getHead_pic()).into(evaluateViewHolder.userHead);
//                    evaluateViewHolder.userName.setText(commentBean.getNickName());
//                    evaluateViewHolder.evaluateContent.setText(commentBean.getContent());
//                    evaluateViewHolder.evaluateDate.setText(DateUtils.getTheDayFromNow(commentBean.getCreate_date()));//((DateUtils.transferLongToDate("yyyy-MM-dd hh:MM", commentBean.getCreateDate())));
//                    evaluateViewHolder.likeText.setText(String.valueOf(commentBean.getPraisenum()));
//
////                    if (commentBean.getIsLike() == 0) {
////                        //没点赞呢
////                        evaluateViewHolder.likeText.setCompoundDrawablesWithIntrinsicBounds(viewHolder.itemView.getContext().getResources().getDrawable(R.mipmap.ic_unlike), null, null, null);
////                    } else {
////                        //已经点过了
////                        evaluateViewHolder.likeText.setCompoundDrawablesWithIntrinsicBounds(viewHolder.itemView.getContext().getResources().getDrawable(R.mipmap.ic_liked), null, null, null);
////                    }
//                }
//
//                break;
//            case TYPE_RECOMMEND:
//                // 推荐List
//                final List<TypeBean> list = (List<TypeBean>) mList.get(i);
//
//                RecommendHolder watchingItemHolder = (RecommendHolder) viewHolder;
//                watchingItemHolder.recyclerView.setAdapter(new RecyclerView.Adapter() {
//                    @NonNull
//                    @Override
//                    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//                        return new ItemHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail_watching_item, viewGroup, false));
//                    }
//
//                    @Override
//                    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
//                        ItemHolder itemHolder = (ItemHolder) viewHolder;
//                        TypeBean bean = list.get(i);
//                        Glide.with(viewHolder.itemView)
//                                .applyDefaultRequestOptions(new RequestOptions().placeholder(R.mipmap.ic_place)
//                                .error(R.mipmap.ic_place))
//                                .load(bean.getCoverUrl())
//                                .transition(DrawableTransitionOptions.withCrossFade())
//                                .into(itemHolder.bookPic);
//                        itemHolder.bookName.setText(bean.getNovelName());
//                        itemHolder.bookAuthor.setText(bean.getAuthorName());
//                    }
//
//                    @Override
//                    public int getItemCount() {
//                        return list == null ? 0 : list.size();
//                    }
//
//                    class ItemHolder extends ViewHolder {
//                        ImageView bookPic;
//                        TextView bookName;
//                        TextView bookAuthor;
//
//                        ItemHolder(@NonNull View itemView) {
//                            super(itemView);
//                            bookPic = itemView.findViewById(R.id.book_pic);
//                            bookName = itemView.findViewById(R.id.book_title);
//                            bookAuthor = itemView.findViewById(R.id.book_author);
//                            itemView.findViewById(R.id.detail_watching_item).setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    listener.onItemClicked(BookDetailAdapter.this, v, getAdapterPosition());
//                                }
//                            });
//                        }
//                    }
//                });
//                break;
//        }
//    }
//
//    @Override
//    public int getItemCount() {
//        return mList == null ? 0 : mList.size();
//    }
//
//    @Override
//    public int getItemViewType(int position) {
//        switch (position) {
//            case 0:
//                return TYPE_BASIC_INFO;
//            case 1:
//                return TYPE_COMMENT;
//            case 2:
//                return TYPE_RECOMMEND;
//            default:
//                return TYPE_RECOMMEND;
//        }
//    }
//
//    class BaseInfoViewHolder extends ViewHolder {
//        ImageView bookBg;
//        ImageView bookPic;
//        TextView bookTitle;
//        TextView bookSub;
//        TextView bookWordNum;
//        TextView parentCategory;
//        TextView category;
////        ExpandableTextView descText;//书简介
//
//        //最新
//        LinearLayout updateParent;
//        TextView updateCategory;//最新章节
//        TextView updateTime;//最新的更新时间
//
//        //ic_catalogue
//        LinearLayout commentParent;//ic_comment_write
//        TextView commentSum;//更新总章数
//        TextView commentTime;//目录时间
//
//
//        BaseInfoViewHolder(@NonNull View itemView) {
//            super(itemView);
//            bookBg = itemView.findViewById(R.id.bg_image);
//            bookPic = itemView.findViewById(R.id.book_pic);
//            bookTitle = itemView.findViewById(R.id.book_title);
//            bookTitle.getPaint().setFakeBoldText(true);
//            bookSub = itemView.findViewById(R.id.book_sub);
//            bookWordNum = itemView.findViewById(R.id.book_word_num);
//            parentCategory = itemView.findViewById(R.id.parent_category_ame);
//            category = itemView.findViewById(R.id.category);
//
////            descText = itemView.findViewById(R.id.desc_text);
//
//            //最新
//            updateParent = itemView.findViewById(R.id.detail_update_time);
//            updateCategory = itemView.findViewById(R.id.update_category);
//            updateTime = itemView.findViewById(R.id.last_update_time);
//
//            //ic_catalogue
//            commentParent = itemView.findViewById(R.id.detail_comment);
//            commentSum = itemView.findViewById(R.id.category_sum);
//            commentTime = itemView.findViewById(R.id.update_time);
//
//            commentParent.setOnClickListener(l);
//            updateParent.setOnClickListener(l);
//        }
//
//        View.OnClickListener l = new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                listener.onItemClicked(BookDetailAdapter.this, v, getAdapterPosition());
//            }
//        };
//    }
//
//    //书评
//    class EvaluateViewHolder extends ViewHolder {
//
//        TextView inputComment;
//        ImageView userHead;//评论者头像
//        TextView userName;//评论者名字
//        TextView evaluateContent;
//        TextView likeText;
//        TextView evaluateDate;
//        TextView moreEvaluate;
//        TextView noCommentText;
//
//        View.OnClickListener onClickListener = new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                listener.onItemClicked(BookDetailAdapter.this, v, getAdapterPosition());
//            }
//        };
//
//
//        EvaluateViewHolder(@NonNull View itemView) {
//            super(itemView);
//            inputComment = itemView.findViewById(R.id.input_comment_text);
//            userHead = itemView.findViewById(R.id.user_head);
//            userName = itemView.findViewById(R.id.user_name);
//            evaluateContent = itemView.findViewById(R.id.evaluate_content);
//            likeText = itemView.findViewById(R.id.like_text);
//            evaluateDate = itemView.findViewById(R.id.evaluate_date);
//            moreEvaluate = itemView.findViewById(R.id.more_evaluate);
//            noCommentText = itemView.findViewById(R.id.no_comment_text);
//
//            inputComment.setOnClickListener(onClickListener);
//            likeText.setOnClickListener(onClickListener);
//            moreEvaluate.setOnClickListener(onClickListener);
//        }
//    }
//
//    class RecommendHolder extends ViewHolder {
//        RecyclerView recyclerView;
//
//        RecommendHolder(@NonNull View itemView) {
//            super(itemView);
//            recyclerView = itemView.findViewById(R.id.horizontal_recycler_view);
//            LinearLayoutManager manager = new LinearLayoutManager(itemView.getContext());
//            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
//            recyclerView.setLayoutManager(manager);
//        }
//    }
//
//    public List<Object> getList() {
//        return mList;
//    }
//
//    public void setList(List<Object> mList) {
//        this.mList = mList;
//    }
//}
